package cn.itcast.eduservice.controller;


import cn.itcast.baseservice.exceptionhandle.GuliException;
import cn.itcast.commonutils.R;
import cn.itcast.eduservice.entity.EduChapter;
import cn.itcast.eduservice.entity.chapter.ChapterVo;
import cn.itcast.eduservice.service.EduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Duanzihuang
 * @since 2020-05-19
 */
@Api(description = "章节管理")
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
public class EduChapterController {

    @Autowired
    private EduChapterService eduChapterService;

    @ApiOperation(value = "根据课程id获取章节信息")
    @GetMapping("getChapterVideo/{courseId}")
    public R getChapterVideoById(@PathVariable String courseId) {
        List<ChapterVo> chapterVoList = eduChapterService.getChapterVideoById(courseId);
        return R.ok().data("list",chapterVoList);
    }

    @ApiOperation(value="添加章节")
    @PostMapping("addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter) {
        eduChapterService.save(eduChapter);
        return R.ok();
    }

    @ApiOperation(value = "获取章节信息")
    @GetMapping("getChapter/{id}")
    public R getChapterById(@PathVariable String id) {
        EduChapter chapter = eduChapterService.getById(id);
        return R.ok().data("chapter",chapter);
    }

    @ApiOperation(value="修改章节")
    @PostMapping("updateChapter")
    public R updateChapter(@RequestBody EduChapter eduChapter) {
        eduChapterService.updateById(eduChapter);
        return R.ok();
    }

    @ApiOperation(value="删除章节")
    @DeleteMapping("deleteChapter}")
    public R deleteChapter(@PathVariable String chapterId) {
        boolean b = eduChapterService.removeById(chapterId);
        if (!b) {
            throw new GuliException(20001,"删除失败");
        }
        return R.ok();
    }
}

